package cn.tedu.ivos.dict.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.utils.KeyGenerator;
import cn.tedu.ivos.dict.dao.mapper.DictItemMapper;
import cn.tedu.ivos.dict.dao.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictItemQueryParam;
import cn.tedu.ivos.dict.pojo.dto.DictItemSaveParam;
import cn.tedu.ivos.dict.pojo.dto.DictQueryParam;
import cn.tedu.ivos.dict.pojo.entity.DictItem;
import cn.tedu.ivos.dict.pojo.vo.DictItemVO;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dict.service.DictItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictItemServiceImpl implements DictItemService {
    @Autowired
    private DictItemMapper dictItemMapper;
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<DictItemVO> selectDictItem(DictItemQueryParam dictOptionQuery) {
        log.debug("查询字典项业务，dictOptionQuery={}",dictOptionQuery);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = KeyGenerator.generateKey(getClass().getName(),"selectDictItem",dictOptionQuery.toString());
        List<DictItemVO> list = (List<DictItemVO>) valueOperations.get(key);
        if(list!=null){
            System.out.println("data from redis");
            return list;
        }
        System.out.println("data from mysql");
        list = dictItemMapper.selectDictItem(dictOptionQuery);
        valueOperations.set(key,list);
        return list;
    }

    @Override
    public void saveDictItem(DictItemSaveParam dictItemSaveParam) {
        log.debug("保存字典项业务，dictOptionSaveParam={}",dictItemSaveParam);
        DictItem dictOption = new DictItem();
        BeanUtils.copyProperties(dictItemSaveParam,dictOption);
        if(dictOption.getId()==null){
            dictOption.setCreateTime(new Date());
            dictItemMapper.insert(dictOption);
        }else{
            dictOption.setUpdateTime(new Date());
            dictItemMapper.update(dictOption);
        }
    }

    @Override
    public List<DictItemVO> selectDictItemByCode(String code) {
        log.debug("根据字典编码查询字典项业务，code={}",code);
        //先查询该字典是否存在，若存在，再查询该字典code下对应的字典项
        DictQueryParam dictQuery = new DictQueryParam();//封装字典的查询对象
        dictQuery.setCode(code);//给字典查询对象设置字典编码
        List<DictVO> dictVOList = dictMapper.selectDictByCode(dictQuery);//查字典
        if(dictVOList!=null && dictVOList.size()>0){//如果根据code查到了字典
            DictVO dictVO = dictVOList.get(0);//获取当前字典对象
            DictItemQueryParam dictOptionQuery = new DictItemQueryParam();//封装字典项查询对象
            dictOptionQuery.setDictId(dictVO.getId());//给字典项查询对象设置字典id
            //根据字典项id查询该字典下对应的所有字典项,并返回
            List<DictItemVO> dictOptionVOList = dictItemMapper.selectDictItem(dictOptionQuery);
            return dictOptionVOList;
        }else{
            //如果根据字典编码code查不到字典,就抛出“数据不存在”的业务层异常
            throw new ServiceException("字典不存在");
        }
    }

    @Override
    public void deleteDictItem(Long id) {
        log.debug("删除字典项业务：id={}",id);
        dictItemMapper.deleteById(id);
    }
}